#include "client.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QHostAddress>

Client::Client(QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *topLayout = new QHBoxLayout;
    QLabel *ipLabel = new QLabel(tr("IP: "), this);
    ipLineEdit = new QLineEdit(this);
    ipLineEdit->setText("127.0.0.1");
    QLabel *portLabel = new QLabel(tr("Port: "), this);
    portLineEdit = new QLineEdit(this);
    portLineEdit->setText("1234");

    topLayout->addWidget(ipLabel);
    topLayout->addWidget(ipLineEdit);
    topLayout->addWidget(portLabel);
    topLayout->addWidget(portLineEdit);

    recvTextEdit = new QTextEdit(this);
    recvTextEdit->setEnabled(false);
    sendLineEdit = new QLineEdit(this);
    sendLineEdit->setFocus();
    connect(sendLineEdit, SIGNAL(editingFinished()),
            this, SLOT(sendMsg()));

    QHBoxLayout *bottomLayout = new QHBoxLayout;
    statusLabel = new QLabel(tr("No connected."), this);
    sendBtn = new QPushButton(tr("Send"), this);
    sendBtn->setEnabled(false);
    connectBtn = new QPushButton(tr("Connect"), this);
    bottomLayout->addWidget(statusLabel);
    bottomLayout->addStretch();
    bottomLayout->addWidget(sendBtn);
    bottomLayout->addWidget(connectBtn);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(recvTextEdit);
    mainLayout->addWidget(sendLineEdit);
    mainLayout->addLayout(bottomLayout);

    setLayout(mainLayout);
    connect(connectBtn, SIGNAL(clicked()), this, SLOT(connectServer()));
    connect(sendBtn, SIGNAL(clicked()), this, SLOT(sendMsg()));
}

Client::~Client()
{

}

void Client::connectServer()
{
    QString address = ipLineEdit->text();
    QString port = portLineEdit->text();
    if (address.isEmpty() || port.isEmpty()){
        QMessageBox::warning(this, tr("Info Error"), tr("Address or port is empty"),
                             QMessageBox::Cancel, QMessageBox::Ok);
        return;
    }else{
        m_tcpSocket = new QTcpSocket(this);
        m_tcpSocket->connectToHost(QHostAddress(address),quint16(port.toInt()));
        connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(serverConnected()));
        connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(readMsg()));
        connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(serverDisconnected()));
    }
}

void Client::readMsg()
{
    QByteArray buffer = m_tcpSocket->readAll();
    recvTextEdit->append("He: " + QVariant(buffer).toString());
}

void Client::sendMsg()
{
    QString text = sendLineEdit->text();
    if (text.isEmpty())
        return;
    else
        m_tcpSocket->write(text.toStdString().c_str(),
                           strlen(text.toStdString().c_str()));
    recvTextEdit->append("Me: " + text);
    sendLineEdit->clear();
}

void Client::serverConnected()
{
    statusLabel->setText(tr("Connected"));
    sendBtn->setEnabled(true);
    connectBtn->setEnabled(false);
}

void Client::serverDisconnected()
{
    statusLabel->setText(tr("No Connection...."));
    sendBtn->setEnabled(false);
    connectBtn->setEnabled(true);
}

